<template>
  <div class="form-div">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-input v-model="form.tag" placeholder="请输入标签"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input v-model="form.type" placeholder="请输入类型"></el-input>
      </el-form-item>
      <el-form-item label="父级ID" prop="pid">
        <el-input v-model="form.pid" placeholder="请输入父级ID" :value="0"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save(formRef)">保存</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import {ElMessage} from "element-plus";
import {editLabelApi} from "@/assets/api/system/label";
import {ref} from "vue";

const props = defineProps({
  form: {}
});

const emit = defineEmits(['save', 'close']);

// 表单验证
const formRef = ref();
const rules = {
  name: [
    {required: true, message: '请输入名称', trigger: 'blur'},
  ],
  tag: [
    {required: true, message: '请输入标签', trigger: 'blur'},
  ],
  type: [
    {required: true, message: '请输入类型', trigger: 'blur'},
  ]
}

// 保存
const save = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      editLabelApi(props.form).then(res => {
        if (res.data.code === 200) {
          ElMessage.success('保存成功');
          emit('save', true);
        } else {
          ElMessage.error(res.data.msg);
        }
      })
    }
  })
}
</script>

<style scoped>

</style>